/**************************************************************************/
/*  Copyright (C) School of Software Engineering of USTC, 2012            
/*                                                                        
/*  FILE NAME: util.h                                                    
/*  PRINCIPAL AUTHOR: SA12226114(Xin Zhu), SA12226139(Dan Shu)            
/*  SUBSYSTEM NAME:                                                     
/*  MODULE NAME: util                                                    
/*  LANGUAGE: C                                                           
/*  TARGET ENVIRONMENT: Linux                                             
/*  DATE OF FIRST RELEASE: 2012-11-20                                     
/*  DESCRIPTION: Impement of Abstract Interface for Tokyo Cabinet DB API  
/**************************************************************************/


#include "common.h"
#include "util.h"


#define ENTER_ASCII 10  /* ACSII for enter */
#define SPACE_ASCII 32  /* ACSII for space */

/*****************************************************************
Function Name: util_init
Description: init string buffer
Parameter:  

@mem: a pointer to memory need to be initialized
@size: size of memory 

Author: SA12226114(Xin Zhu), SA12226139(Dan Shu)
Date: 2012-11-27
******************************************************************/
void util_init(char *mem, int size)
{
    if(!mem)
    {
        printf("pointer is null\n");

        return;
    }
    
    memset(mem, ' ', size);

    return;
}


/*****************************************************************
Function Name: util_menu
Description: display menu
Parameter:  void
Author: SA12226114(Xin Zhu), SA12226139(Dan Shu)
Date: 2012-11-27
******************************************************************/
void util_menu()
{
    printf("--------------------------------Welcome! please enter your choice-----------------------------------------\n");
    printf("                                open - open database                                                      \n");
    printf("                                set - set key and value into database                                     \n");
    printf("                                get - get value from database                                             \n");
    printf("                                remove - remove a record from database                                    \n");
    printf("                                ls - query all data from database                                         \n");
    printf("                                close - close database                                                    \n");
    printf("                                delete - delete database                                                  \n"); 
    printf("                                help - menu info                                                          \n"); 
    printf("                                quit - quit tool                                                          \n"); 
    printf("----------------------------------------------------------------------------------------------------------\n");

    return;
}


/*****************************************************************
Function Name: util_getcommandline
Description: get command line from console
Parameter:  

@cmdline: a pointer to a buffer perserves command line which is from console
@size: buffer size

Author: SA12226114(Xin Zhu), SA12226139(Dan Shu)
Date: 2012-11-27
******************************************************************/
void util_getcommandline(char *cmdline, int size)
{
    fgets(cmdline, size, stdin);
    
    return;
}


/*****************************************************************
Function Name: util_parse
Description: parse command line
Parameter:  

@string: a pointer to a buffer perserves command line which is from console
@strlen: command line length
@argv0: a pointer to a buffer, which perserves command.
@argv1: a pointer to a buffer, which perserves key.
@argv2: a pointer to a buffer, which perserves value.

Author: SA12226114(Xin Zhu), SA12226139(Dan Shu)
Date: 2012-11-27
******************************************************************/
void util_parse(char *string, int strlen, char *argv0, char *argv1, char *argv2)
{
    char *start, *move, *end;
    int times = 1, length;
    
    start = move = string;

    /* parse command from cmdline and put cmd into argv0, key into argv1, value into argv2 */
    for(; move < (string + strlen); move++)
    {
        /* space and enter both are ending sign */
        if((*move) != SPACE_ASCII && (*move) != ENTER_ASCII)
            continue;

        /* put corresponding string into corresponding buffer */
        end = (move - 1);
        if(times == 1)
        {   
            strncpy(argv0, start, (end - start + 1));
        }
        else if(times == 2)
        {            
            strncpy(argv1, start, (end - start + 1));
        }
        else if(times == 3)
        {
            strncpy(argv2, start, (end - start + 1));
        }
        
        times++;

        /* space and enter both are ending sign */
        if(*(move + 1) == SPACE_ASCII || *(move + 1) == ENTER_ASCII)
            return;

        start = move + 1;
        move++;
    }
     
    return;
}


/*****************************************************************
Function Name: util_memset
Description: format buffer
Parameter:  

@str: a pointer to a buffer perserves command line which is from console
@length: string length

Author: SA12226114(Xin Zhu), SA12226139(Dan Shu)
Date: 2012-11-27
******************************************************************/
void util_memset(char *str, int length)
{
    char *move;
    int index;

    move = str;
        
    for(index = 0; index < length; index++)
    {
        if(*move == SPACE_ASCII)
            memset(move, '\0', 1);

        move++;
    }

    return;
}


/*****************************************************************
Function Name: util_format
Description: format command line
Parameter:  

@cmdline: a pointer to a buffer perserves command line which is from console
@cmd: a pointer to a buffer, which perserves command.
@key: a pointer to a buffer, which perserves key.
@value: a pointer to a buffer, which perserves value.

Author: SA12226114(Xin Zhu), SA12226139(Dan Shu)
Date: 2012-11-27
******************************************************************/
void util_format(char *cmdline, char *cmd, char *key, char *value)
{
    /* initialize space for recieve cmd, key and value. */
    util_init(cmdline, CMDLINE_LEN);
    util_init(cmd, CMD_LEN);
    util_init(key, KEY_LEN);
    util_init(value, VALUE_LEN);
    
    printf(">>>");
    
    /* get command line */
    util_getcommandline(cmdline, CMDLINE_LEN);
    
    /* parse command line */
    util_parse(cmdline, CMDLINE_LEN, cmd, key, value);
    util_memset(cmd, CMD_LEN);
    util_memset(key, KEY_LEN);
    util_memset(value, VALUE_LEN);

    return;
}
